package net.sudot.chess.business.service;

import net.sudot.chess.business.dao.SigninRecordDao;
import net.sudot.chess.business.model.Customer;
import net.sudot.chess.business.model.PointRecord;
import net.sudot.chess.business.model.Signin;
import net.sudot.chess.business.model.SigninRecord;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;

/**
 * 签到
 *
 * @author tangjialin on 2018-03-05.
 */
@Service
public class SigninRecordService extends BaseService<SigninRecord> {

    @Resource
    private SigninRecordDao signinRecordDao;
    @Resource
    private SigninService signinService;
    @Resource
    private CustomerService customerService;

    /**
     * 添加一个签到记录
     *
     * @param customer 签到客户
     * @return 返回签到结果.true:签到成功;false:签到失败
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Customer customer) {
        LocalDate currentDate = LocalDate.now();
        Long customerId = customer.getId();
        // 获取客户签到信息
        Signin signin = signinService.findByCustomer(customerId);
        signin = signin == null ? new Signin() : signin;
        signin.setCustomerId(customerId);
        // 这里表示上一次签到的时间
        LocalDate lastSigninDate = signin.getLastModifiedDate() == null ? null : signin.getLastModifiedDate().atZone(ZoneId.systemDefault()).toLocalDate();
        // 连续签到天数
        Integer continuous = signin.getContinuous();
        // 签到总天数
        Integer total = signin.getTotal();

        if (lastSigninDate == null) {
            // 没有上一次签到时间表示从未签过到
            continuous = 0;
            total = 0;
        } else {
            // 若上次签到时间和系统时间相比较是同一天,或在系统时间之后,则表示今天已签到
            // 例如:
            // lastSigninDate=2018年03月16日,currentDate=2018年03月15日,则已签到
            // lastSigninDate=2018年03月16日,currentDate=2018年03月16日,则已签到
            // lastSigninDate=2018年03月16日,currentDate=2018年03月17日,则未签到
//            int compareTo = DateUtils.truncatedCompareTo(lastSigninDate, currentDate, Calendar.DAY_OF_MONTH);
            int compareTo = lastSigninDate.compareTo(currentDate);
            if (compareTo >= 0) { return false; }
//            Instant date = DateUtils.addDays(lastSigninDate, 1);
//            if (DateUtils.isSameDay(date, currentDate)) {
            if (currentDate.equals(lastSigninDate.plus(1, ChronoUnit.DAYS))) {
                // 上次签到时间就是本次签到前一天,则维持原连续签到天数
                continuous = continuous == null ? 0 : continuous;
            } else {
                // 上次签到时间加一天得到的时间若不等于今天,则签到已不连续,重置连续签到天数
                continuous = 0;
            }
            total = total == null ? 0 : total;
        }
        // 连续签到天数统一加1
        signin.setContinuous(continuous + 1);
        signin.setTotal(total + 1);
        signinService.saveOrUpdate(signin);
        customerService.addPoint(customer, SigninRecord.POINT, PointRecord.Type.SIGN, null);

        save(new SigninRecord().setCustomerId(customerId));
        return true;
    }
}
